<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryRef"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="药品名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入药品名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['index']"
          >新增药品</el-button
        >
      </el-col>
      <right-toolbar
        v-model:showSearch="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="stockList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="药品编码" align="center" prop="code" />
      <el-table-column label="药品名称" align="center" prop="name" />
      <el-table-column label="药品单价" align="center" prop="price" />
      <el-table-column label="药品进价" align="center" prop="buyprice" />
      <el-table-column label="库存" align="center">
        <template #default="scope">
          <span style="color: red">{{ scope.row.count }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template #default="scope">
          <el-button
            link
            type="primary"
            icon="Download"
            @click="handleStockIn(scope.row)"
            v-hasPermi="['index']"
            >入库</el-button
          >
          <el-button
            link
            type="primary"
            icon="Upload"
            @click="handleStockOut(scope.row)"
            v-hasPermi="['index']"
            >出库</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加药品库存对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="stockRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="药品名称" prop="name">
          <el-input v-model="form.name" placeholder="请输入药品名称" />
        </el-form-item>
        <el-form-item label="药品单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入药品单价" />
        </el-form-item>
        <el-form-item label="药品进价" prop="buyprice">
          <el-input v-model="form.buyprice" placeholder="请输入药品进价" />
        </el-form-item>
        <el-form-item label="库存" prop="count">
          <el-input v-model="form.count" placeholder="请输入库存" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 入库对话框 -->
    <el-dialog title="药品入库" v-model="inDialogVisible" width="400px">
      <el-form :model="inForm" label-width="80px">
        <el-form-item label="入库数量">
          <el-input-number v-model="inForm.count" :min="1" :step="1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="inDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmStockIn">确 定</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 出库对话框 -->
    <el-dialog title="药品出库" v-model="outDialogVisible" width="400px">
      <el-form
        :model="outForm"
        :rules="rules.outForm"
        ref="outFormRef"
        label-width="80px"
      >
        <el-form-item label="出库数量" prop="count">
          <el-input-number v-model="outForm.count" :min="1" :step="1" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="outDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="confirmStockOut">确 定</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Stock">
import { alertlistStock, addStock, stockIn, stockOut } from "@/api/drug/stock";
import { getCurrentInstance, ref, reactive, toRefs } from "vue";

const { proxy } = getCurrentInstance();

const stockList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const total = ref(0);
const title = ref("");
const currentStock = ref(0);

// 入库相关状态
const inDialogVisible = ref(false);
const inForm = ref({
  code: null,
  count: 1,
});

// 出库相关状态
const outDialogVisible = ref(false);
const outForm = ref({
  code: null,
  count: 1,
});

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    name: null,
    count: null,
  },
  rules: {
    outForm: {
      count: [
        { required: true, message: "出库数量不能为空", trigger: "blur" },
        {
          validator: (rule, value, callback) => {
            if (value > currentStock.value) {
              callback(new Error("出库数量不能大于当前库存"));
            } else {
              callback();
            }
          },
          trigger: "blur",
        },
      ],
    },
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询药品库存列表 */
function getList() {
  loading.value = true;
  alertlistStock(queryParams.value).then((response) => {
    stockList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    code: null,
    name: null,
    price: null,
    buyprice: null,
    count: null,
  };
  proxy.resetForm("stockRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加药品库存";
}

/** 入库操作 */
function handleStockIn(row) {
  inForm.value = {
    code: row.code,
    count: 1,
  };
  inDialogVisible.value = true;
}

/** 确认入库 */
function confirmStockIn() {
  stockIn(inForm.value).then(() => {
    console.log(inForm.value);
    proxy.$modal.msgSuccess("入库成功");
    inDialogVisible.value = false;
    getList();
  });
}

/** 出库操作 */
function handleStockOut(row) {
  outForm.value = {
    code: row.code,
    count: 1,
  };
  currentStock.value = row.count; // 设置当前库存
  outDialogVisible.value = true;
}

/** 确认出库 */
function confirmStockOut() {
  proxy.$refs["outFormRef"].validate((valid) => {
    if (valid) {
      stockOut(outForm.value).then(() => {
        proxy.$modal.msgSuccess("出库成功");
        outDialogVisible.value = false;
        getList();
      });
    }
  });
}

/** 提交新增表单 */
function submitForm() {
  proxy.$refs["stockRef"].validate((valid) => {
    if (valid) {
      addStock(form.value).then((response) => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

getList();
</script>
